package com.example.ormsqlsplicingframe1.sql.impl;

import com.example.ormsqlsplicingframe1.sql.ConditionSql;
import io.swagger.models.auth.In;

import java.util.*;
import java.util.function.Consumer;

public class ConditionImpl implements ConditionSql {

    private StringJoiner sb = new StringJoiner(" ");

    private ArrayList values = new ArrayList();


    private String oper = "and";

    public ConditionImpl() {
    }

    public ConditionImpl(String oper) {
        this.oper = oper;
    }

    @Override
    public ConditionSql sql(String sql) {

        addOper();
        sb.add(sql);
        return this;
    }

    private void addSql(String sql, Object value){
        if (isNull(value)){
            return ;
        }
        addOper();
        sb.add(sql);

        values.add(value);
    }

    @Override
    public ConditionSql like(String name, Object value) {

        addSql(name+"% like ? %",value);

        return this;
    }

    @Override
    public ConditionSql rightLike(String name, Object value) {

        addSql(name+"% like ?",value);

        return this;
    }

    @Override
    public ConditionSql leftLike(String name, Object value) {

        addSql(name+" like ? %",value);

        return this;
    }

    private void addOper(){
        if(values.size() >0){
            sb.add(oper);
        }
    }


    private boolean isNull(Object value){
        if (value instanceof String){
            if (value ==""){
                return true;
            }
            return false;
        }else if (value instanceof Collection){
            Collection coll = (Collection)value;
            if (coll.size() == 0){
                return true;
            }
            return false;
        }
        else{
            return value == null;
        }
    }

    @Override
    public ConditionSql or(Consumer<ConditionSql> consumer) {

        ConditionImpl condition =  new ConditionImpl("or");

        consumer.accept(condition);

        if(condition.getValues().size() > 0){
            addOper();
            sb.add("("+condition.getSql()+")");

            values.addAll(condition.getValues());
        }


        return this;
    }

    @Override
    public ConditionSql in(String name, Collection value) {

        if(isNull(value)){
            return this;
        }

        addOper();
        // name like %value%  ？ adn or name in (?,?,?)

        final StringJoiner joiner = new StringJoiner(" ,");

        for (Object o : value) {
            joiner.add("?");
            values.add(o);
        }



        sb.add(name+" in ( "+joiner+" )");


        return this;
    }

    @Override
    public ConditionSql eq(String name, Object value) {// iid == ss



        addSql(name +" = ?",value);

        return this;
    }



    @Override
    public ConditionSql ne(String name, Object value) {
        addSql(name +" != ?",value);

        return this;
    }

    @Override
    public String getSql() {
        return sb.toString();
    }

    @Override
    public List<Object> getValues() {
        return values;
    }
}
